package conf2.model;

import conf2.model.EActor.Side;

class SpecialCardLogic
{

    static boolean canPlayAKingRevealed(WorldState ws)
    {
        if (ws.isWatcherRevealed() &&
                ws.isAlive(EActor.V_WATCHER) &&
                ws.countLiving(Side.DARK) == 1) {
            return false;
        }

        return ws.isAlive(EActor.C_ARAGORN) || ws.isAlive(EActor.V_ARAGORN);
    }

    static boolean canPlayCrebainOfDunland(WorldState ws)
    {
        return true;
    }

    static boolean canPlayGandalfTheWhite(WorldState ws)
    {
        int inFangorn = ws.getOccupantCount(ETerritory.FANGORN);
        if (ws.getOccupyingSide(ETerritory.FANGORN) == Side.DARK) {
            return false;
        }
        boolean gandalfIsDead = !ws.isAlive(EActor.C_GANDALF) && !ws.isAlive(EActor.V_GANDALF);
        return gandalfIsDead && inFangorn < 2;
    }

    // TODO: NEEDS TESTING!
    static boolean canPlayTheDarkOfMordor(WorldState ws)
    {
        // Count dark actors that can move forward. If they are 2 or more, the card can be played.
        // The ones that can't move are:
        // 1) revealed watcher
        // 2) character in Shire (if not classic Flying Nazgul)

        int movableCount = 0;
        for (EActor ea : ws.remainingActors(Side.DARK)) {
            if (ea == EActor.V_WATCHER && !ws.isWatcherRevealed()) {
                movableCount++;
            } else if (ea == EActor.C_F_NAZGUL) {
                movableCount++;
            } else if (ws.whereIs(ea) != ETerritory.SHIRE) {
                movableCount++;
            }
        }
        return movableCount > 1;
    }

    static boolean canPlayGwaihirTheWindlord(WorldState ws)
    {
        return true;
    }

    static boolean canPlayRecallToMordor(WorldState ws)
    {
        int inMordor = ws.getOccupantCount(ETerritory.MORDOR);

        if (ws.getOccupyingSide(ETerritory.MORDOR) == Side.LIGHT) {
            return false;
        }

        if (inMordor < 4 && ws.countLiving(Side.DARK) > inMordor) {
            return true;
        }
        return false;
    }

    static boolean canPlayShadowfax(WorldState ws)
    {
        return true;
    }

    static boolean canPlayPalantir(WorldState ws)
    {
        for (ETerritory et : ETerritory.values()) {
            if (ws.getOccupyingSide(et) == Side.LIGHT &&
                    et != ETerritory.SHIRE) {
                return true;
            }
        }
        return false;
    }
}
